﻿@page "/"
@using Microsoft.EntityFrameworkCore
@using Wms.Core.Entities
@inject Wms.Data.WarehouseDbContext Db

<div class="container-fluid py-4">
    <h4 class="text-primary mb-4">
        <span class="oi oi-home me-2"></span> 仓库管理系统
    </h4>

    <div class="row g-4">
        <!-- 仓库管理 -->
        <div class="col-12 col-md-4">
            <NavLink href="/warehouses" class="text-decoration-none">
                <div class="card shadow-sm border-warning h-100 hover-effect">
                    <div class="card-body d-flex flex-column">
                        <div class="d-flex align-items-center mb-3">
                            <span class="oi oi-home text-warning me-2" style="font-size: 1.5rem;"></span>
                            <h5 class="card-title text-warning mb-0">仓库管理</h5>
                        </div>
                        <p class="card-text text-muted mb-2">统一管理所有仓库信息</p>
                        <div class="mt-auto">
                            <small class="text-muted">共 <strong>@warehouseTotal</strong> 个仓库</small>
                        </div>
                    </div>
                </div>
            </NavLink>
        </div>

        <!-- 库位管理 -->
        <div class="col-12 col-md-4">
            <NavLink href="/locations" class="text-decoration-none">
                <div class="card shadow-sm border-info h-100 hover-effect">
                    <div class="card-body d-flex flex-column">
                        <div class="d-flex align-items-center mb-3">
                            <span class="oi oi-map-marker text-info me-2" style="font-size: 1.5rem;"></span>
                            <h5 class="card-title text-info mb-0">库位管理</h5>
                        </div>
                        <p class="card-text text-muted mb-2">精细化管理存储位置</p>
                        <div class="mt-auto">
                            <small class="text-muted">共 <strong>@locationTotal</strong> 个库位</small>
                        </div>
                    </div>
                </div>
            </NavLink>
        </div>

        <!-- 物料管理 -->
        <div class="col-12 col-md-4">
            <NavLink href="/items" class="text-decoration-none">
                <div class="card shadow-sm border-primary h-100 hover-effect">
                    <div class="card-body d-flex flex-column">
                        <div class="d-flex align-items-center mb-3">
                            <span class="oi oi-tag text-primary me-2" style="font-size: 1.5rem;"></span>
                            <h5 class="card-title text-primary mb-0">物料管理</h5>
                        </div>
                        <p class="card-text text-muted mb-2">查看和管理所有物料信息</p>
                        <div class="mt-auto">
                            <small class="text-muted">共 <strong>@itemTotal</strong> 种物料</small>
                        </div>
                    </div>
                </div>
            </NavLink>
        </div>

        <!-- 库存管理 -->
        <div class="col-12 col-md-4">
            <NavLink href="/inventory" class="text-decoration-none">
                <div class="card shadow-sm border-secondary h-100 hover-effect">
                    <div class="card-body d-flex flex-column">
                        <div class="d-flex align-items-center mb-3">
                            <span class="oi oi-layers text-secondary me-2" style="font-size: 1.5rem;"></span>
                            <h5 class="card-title text-secondary mb-0">库存管理</h5>
                        </div>
                        <p class="card-text text-muted mb-2">实时查看库存情况、批次追踪</p>
                        <div class="mt-auto">
                            <small class="text-muted">总库存: <strong>@totalInventory</strong> 件，预警: <strong>@alertInventory</strong></small>
                        </div>
                    </div>
                </div>
            </NavLink>
        </div>

        <!-- 入库管理 -->
        <div class="col-12 col-md-4">
            <NavLink href="/stockin-list" class="text-decoration-none">
                <div class="card shadow-sm border-success h-100 hover-effect">
                    <div class="card-body d-flex flex-column">
                        <div class="d-flex align-items-center mb-3">
                            <span class="oi oi-action-redo text-success me-2" style="font-size: 1.5rem;"></span>
                            <h5 class="card-title text-success mb-0">入库管理</h5>
                        </div>
                        <p class="card-text text-muted">创建和查看入库单，支持明细追溯</p>
                        <div class="mt-auto">
                            <small class="text-muted">高效管理采购/退货入库</small>
                        </div>
                    </div>
                </div>
            </NavLink>
        </div>

        <!-- 出库管理 -->
        <div class="col-12 col-md-4">
            <NavLink href="/stockout-list" class="text-decoration-none">
                <div class="card shadow-sm border-warning h-100 hover-effect">
                    <div class="card-body d-flex flex-column">
                        <div class="d-flex align-items-center mb-3">
                            <span class="oi oi-action-undo text-warning me-2" style="font-size: 1.5rem;"></span>
                            <h5 class="card-title text-warning mb-0">出库管理</h5>
                        </div>
                        <p class="card-text text-muted">处理生产领料、销售出库等业务</p>
                        <div class="mt-auto">
                            <small class="text-muted">支持批次与库存扣减</small>
                        </div>
                    </div>
                </div>
            </NavLink>
        </div>
    </div>
</div>

<!-- 悬停效果样式 -->
<style>
    .hover-effect {
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    cursor: pointer;
    }

    .hover-effect:hover {
    transform: translateY(-4px);
    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.12);
    }

    .card {
    height: 100%;
    }

    .card-body {
    display: flex;
    flex-direction: column;
    }
</style>

@code {
    private int itemTotal = 0;
    private int warehouseTotal = 0;
    private int locationTotal = 0;
    private int totalInventory = 0;
    private int alertInventory = 0;

    protected override async Task OnInitializedAsync()
    {
        try
        {
            itemTotal = await Db.Items.CountAsync();
            warehouseTotal = await Db.Warehouses.CountAsync();
            locationTotal = await Db.Locations.CountAsync(); 
            totalInventory = await Db.Inventories.SumAsync(i => i.Quantity); 
            alertInventory = await Db.Inventories .Where(i => i.Quantity < i.SafetyStock) .CountAsync();
        }
        catch (Exception ex)
        {
            // 日志记录（可选）
            Console.WriteLine($"首页统计加载失败: {ex.Message}");
            // 失败时保持为 0
        }
    }
}